package happens_before;

/**
 * @author juntao zhang
 */
public class SynchronizedTest {

  public synchronized void test1() {
    System.out.println(Thread.currentThread().getName() + " test1 enter");
    try {
      Thread.sleep(1000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    System.out.println(Thread.currentThread().getName() + " test1 leave");
  }

  public synchronized void test2() {
    System.out.println(Thread.currentThread().getName() + " test2 enter");
    try {
      Thread.sleep(1000);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    System.out.println(Thread.currentThread().getName() + " test2 leave");
  }

  public static void main(String[] args) {
    final SynchronizedTest t = new SynchronizedTest();
    new Thread(new Runnable() {
      @Override
      public void run() {
        System.out.println(Thread.currentThread().getName() + " run in");
        t.test1();
        System.out.println(Thread.currentThread().getName() + " run out");
      }
    }).start();
    try {
      Thread.sleep(100);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    System.out.println(Thread.currentThread().getName() + " run in");
    t.test2();
    System.out.println(Thread.currentThread().getName() + " run out");

  }
}
